//---------------------------------------------------------------------------
// MyStatement Class
// Author : Alex Woo-Hyun Kim at www.webrobot.org
// Email : woorung@webrobot.org,webrobot@hotmail.com,woorung@empal.com
// Date : May 6, 2002
//
// log : May 14, 2002
//	 because of memory leaking, this class is rewritten newly.
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <strings>
#include "CMyStatement.h"
//---------------------------------------------------------------------------
CMyStatement::CMyStatement(char *pszStmt)
{
	m_pszStmt = NULL;
	m_pszFinalStmt = NULL;
	for(int i=0;i<100;i++) m_ppszItemStmt[i] = NULL;

	m_pszStmt = new char[strlen(pszStmt)+1];
	strcpy(m_pszStmt,pszStmt);	
}
//--------------------------------------------------------------------------
CMyStatement::~CMyStatement()
{
	for(int i=0;i<100;i++) {
		if(m_ppszItemStmt[i]!=NULL) delete []m_ppszItemStmt[i];
	}
	delete []m_pszStmt;
	delete []m_pszFinalStmt;
}
//--------------------------------------------------------------------------
void CMyStatement::SetName(int iNamePos,char *pszName)
{
	if(pszName==NULL||!strcmp(pszName,"")) {
		if(m_ppszItemStmt[iNamePos]!=NULL) {
			delete []m_ppszItemStmt[iNamePos];
		}
		m_ppszItemStmt[iNamePos]=NULL;
		return;
	}
	
	if(m_ppszItemStmt[iNamePos]!=NULL) {
		delete []m_ppszItemStmt[iNamePos];
		m_ppszItemStmt[iNamePos] = NULL;
	}

	m_ppszItemStmt[iNamePos] = new char[strlen(pszName)+1];
	strcpy(m_ppszItemStmt[iNamePos],pszName);
}
//--------------------------------------------------------------------------
void CMyStatement::SetString(int iNamePos,char *pszValue)
{
	if(pszValue==NULL||!strcmp(pszValue,"")) {
		if(m_ppszItemStmt[iNamePos]!=NULL) {
                        delete []m_ppszItemStmt[iNamePos];
                }
                m_ppszItemStmt[iNamePos]=NULL;
                return;
        }

	if(m_ppszItemStmt[iNamePos]!=NULL) {
                delete []m_ppszItemStmt[iNamePos];
                m_ppszItemStmt[iNamePos] = NULL;
        }
	m_ppszItemStmt[iNamePos] = new char[strlen(pszValue)+3];
	strcpy(m_ppszItemStmt[iNamePos],"\'");
	strcat(m_ppszItemStmt[iNamePos],pszValue);
	strcat(m_ppszItemStmt[iNamePos],"\'");
}
//--------------------------------------------------------------------------
void CMyStatement::SetInteger(int iNamePos,int iValue)
{
	if(m_ppszItemStmt[iNamePos]!=NULL) {
                delete []m_ppszItemStmt[iNamePos];
                m_ppszItemStmt[iNamePos] = NULL;
        }
	m_ppszItemStmt[iNamePos] = new char[50];
	sprintf(m_ppszItemStmt[iNamePos],"%d",iValue);
}
//--------------------------------------------------------------------------
char *CMyStatement::GetStatement()
{
	int size = 0;
	size += strlen(m_pszStmt); 	
	for(int i=1;i<100;i++) {
		if(m_ppszItemStmt[i]!=NULL) size += strlen(m_ppszItemStmt[i]);
	}

	m_pszFinalStmt = new char[size+1];
	strcpy(m_pszFinalStmt,"");
	
	int count = 1;
	char *pszTmp = m_pszStmt;
	char *pszStart = pszTmp;
	while((pszTmp=strchr(pszTmp,'?'))!=NULL) {
		*pszTmp = '\0';
		pszTmp++;
		strcat(m_pszFinalStmt,pszStart);
		if(m_ppszItemStmt[count]==NULL) {
			count++;	
        	}
		else strcat(m_pszFinalStmt,m_ppszItemStmt[count++]);
		pszStart = pszTmp;
	}
	if(pszStart!=pszTmp) strcat(m_pszFinalStmt,pszStart);
	return m_pszFinalStmt;
}
//--------------------------------------------------------------------------
